package com.minhua.jijiehao2.http

import android.text.TextUtils
import com.google.gson.Gson
import com.minhua.jijiehao2.base.BaseApplication
import com.minhua.jijiehao2.constants.DefineKey
import com.minhua.jijiehao2.utils.AppUtils
import com.minhua.jijiehao2.utils.SPUtils
import com.minhua.jijiehao2.utils.T
import okhttp3.MediaType
import okhttp3.RequestBody
import java.util.*

/**
 * @author moo
 * @date 2017/11/14.
 * @describe 参数
 */
class ParamsMap {
    private val map: TreeMap<String, Any> = TreeMap()

    init {
        clear()
    }

    fun toRequestBody(): RequestBody? {
        val json = Gson()
        val strEntity = json.toJson(map)
        return RequestBody.create(MediaType.parse("application/x-www-form-urlencoded"), strEntity)

    }

    fun get(key: String): Any? = map[key]

    fun put(key: String, value: Any) {
        map.put(key, value)
    }

    fun clear() {
        map.clear()
        map.put(DefineKey.VERSION, AppUtils.getVersion(BaseApplication.APP))
        map.put(DefineKey.MOBILE_TYPE_, DefineKey.MOBILE_TYPE_VALUE_)
        map.put(DefineKey.UPDATE_CHANNEL, AppUtils.getAppMetaData(BaseApplication.APP, "UMENG_CHANNEL"))
        val token = SPUtils.get(DefineKey.TOKEN, "") as String
        val userId = SPUtils.get(DefineKey.USER_ID, "") as String
        if (!TextUtils.isEmpty(token)) {
            map.put(DefineKey.USER_ID, userId)
        }
        if (!TextUtils.isEmpty(token)) {
            map.put(DefineKey.TOKEN, token)
        }

    }

}